package com.zl.smartmall.library.po;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 收货地址
 * @author 陈希然
 *
 */
public class DeliveryAddress {

	private int addressId;
	private int isDefaultAddress;// 1是默认收货地址，0是非默认收货地址
	private String nation;// 中国
	private String province;// 广东省
	private String city;// 深圳市
	private String district;// 南山区
	private String address;// 科苑北路科兴科学园A栋16楼
	private String postcode;
	private String phone;
	private String receiver;// "陈希然"
	private String fullAddress;// "中国广东省深圳市南山区科苑北路科兴科学园A栋16楼"

	public int getAddressId() {
		return addressId;
	}

	public void setAddressId(int addressId) {
		this.addressId = addressId;
	}

	public int getIsDefaultAddress() {
		return isDefaultAddress;
	}

	public void setIsDefaultAddress(int isDefaultAddress) {
		this.isDefaultAddress = isDefaultAddress;
	}

	public String getNation() {
		return nation;
	}

	public void setNation(String nation) {
		this.nation = nation;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getDistrict() {
		return district;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPostcode() {
		return postcode;
	}

	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

	public String getFullAddress() {
		return fullAddress;
	}

	public void setFullAddress(String fullAddress) {
		this.fullAddress = fullAddress;
	}
	
	public String buildJson() throws JSONException {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("address", address);
		jsonObject.put("city", city);
		jsonObject.put("district", district);
		jsonObject.put("fullAddress", fullAddress);
		jsonObject.put("nation", nation);
		jsonObject.put("phone", phone);
		jsonObject.put("postcode", postcode);
		jsonObject.put("province", province);
		jsonObject.put("receiver", receiver);
		jsonObject.put("selected", isDefaultAddress);
		jsonObject.put("id", addressId);
		
		return jsonObject.toString();
		
	}

	public static DeliveryAddress parse(JSONObject jsonObject) throws JSONException {
		DeliveryAddress deliveryAddress = null;
		if(jsonObject != null && jsonObject.length() > 0) {
			deliveryAddress = new DeliveryAddress();
			deliveryAddress.address = jsonObject.getString("address");
			deliveryAddress.city = jsonObject.getString("city");
			deliveryAddress.district = jsonObject.getString("district");
			deliveryAddress.fullAddress = jsonObject.getString("fullAddress");
			deliveryAddress.nation = jsonObject.getString("nation");
			deliveryAddress.phone = jsonObject.getString("phone");
			deliveryAddress.postcode = jsonObject.getString("postcode");
			deliveryAddress.province = jsonObject.getString("province");
			deliveryAddress.receiver = jsonObject.getString("receiver");
			if(jsonObject.has("id")) {
				deliveryAddress.addressId = jsonObject.getInt("id");
			}
			if(jsonObject.has("selected")) {
				deliveryAddress.isDefaultAddress = jsonObject.getInt("selected");
			}
		}
		return deliveryAddress;
	}
	
	public static List<DeliveryAddress> parse(JSONArray jsonArray) throws JSONException {
		List<DeliveryAddress> deliveryAddresses = new ArrayList<DeliveryAddress>();
		for(int i = 0; jsonArray != null && i < jsonArray.length(); i++) {
			DeliveryAddress deliveryAddress = parse(jsonArray.getJSONObject(i));
			if(deliveryAddress != null) {
				deliveryAddresses.add(deliveryAddress);
			}
		}
		
		return deliveryAddresses;
	}
}
